<?php

include_once "common/header.php"; 


// A list of permitted file extensions
$allowed = array('png', 'jpg', 'gif','zip');
$userdir = $_SESSION['home_dir'];
$tmpdir = $userdir ."/blocks/tmp";

if(isset($_FILES['upl']) && $_FILES['upl']['error'] == 0){

	$extension = pathinfo($_FILES['upl']['name'], PATHINFO_EXTENSION);
	if(!file_exists($tmpdir)) {  
      if(!mkdir($tmpdir)) {  
           die("There was a problem. Please try again!");  
      }   
       }  
	   
	if(!in_array(strtolower($extension), $allowed)){
		echo '{"status":"error"}';
		exit;
	}

	if(move_uploaded_file($_FILES['upl']['tmp_name'], $tmpdir .'/'.$_FILES['upl']['name'])){
		echo '{"status":"success"}';
		exit;
	}
}
//Delete dir if user started again
else {
	$dir =$tmpdir;
	 if(file_exists($dir)) {
	 $objects = scandir($dir);
     foreach ($objects as $object) {
       if ($object != "." && $object != "..") {
         if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object);
       }
     }
     reset($objects);
     rmdir($dir);
	 }
   
}

echo '{"status":"error"}';
exit;